package Game;

import java.util.Vector;


public class Hand
{
	private Vector hand; // the cards int the hand
	
	/**
	 * create a Hand object that is initially empty
	 */
	public Hand()
	{
		hand = new Vector();
	}
	/**
	 * discard all cards from the hand, making the hand empty
	 */
	public void clear()
	{
		hand.removeAllElements();
	}
	/**
	 * add the card c to the hand. c should be non-null
	 * (if c is null, nothing is added to the hand
	 * @param c
	 */
	public void addCard(Card c)
	{
		if(c != null)
		{
			hand.addElement(c);
		}
	}
	/**
	 * if the specified card is in the hand, it is removed
	 * @param c
	 */
	public void removeCard(Card c)
	{
		hand.removeElement(c);
	}
	/**
	 * if the specified position is a valid position in the
	 * hand, then the card in that position is removed
	 * @param position
	 */
	public void removeCard(int position)
	{
		if(position >= 0 && position < hand.size())
		{
			hand.removeElementAt(position);
		}
		
	}
	/**
	 * return the number of cards in the hand
	 * @return
	 */
	public int getCardCount()
	{		
		return hand.size();
	}
	/**
	 * Get the card from the hand in given position, where
	 * positions are numbered starting from 0. If the
	 * specified position is not the position number of
	 * a card in the hand, then null is returned
	 * @param position
	 * @return
	 */
	public Card getCard(int position)
	{
		if(position >= 0 && position < hand.size())
		{
			return (Card)hand.elementAt(position);			
		}
		else
		{
			return null;
		}
	}
	/**
	 * sorts the card in the hand so that cards of the same
	 * suit are grouped together. and within a suit the cards
	 * are sorted by value. note that aces are considered
	 * to have the lowest value, 1.
	 */
	public void sortBySuit()
	{
		Vector newHand = new Vector();
		while(hand.size() > 0)
		{
			int pos = 0; // position of minimal card
			Card c = (Card)hand.elementAt(0);//minimal card
			for(int i = 1; i <hand.size();i++)
			{
				Card c1 = (Card)hand.elementAt(i);
				if(c1.getSuit() < c.getSuit() || (c1.getSuit() == c.getSuit() && c1.getValue() < c.getValue()))
				{
					pos = i;
					c = c1;
				}
			}
			hand.removeElementAt(pos);
			newHand.addElement(c);
		}
		hand = newHand;
		
	}
	/**
	 * sorts the cards in the hand so that cards of the same
	 * value are grouped together. cards with the same value
	 * are sorted by suit. note that aces are considered to
	 * have the lowest value, 1.
	 */
	public void sortByValue()
	{
		Vector newHand = new Vector();
		while(hand.size() > 0)
		{
			int pos = 0;
			Card c = (Card)hand.elementAt(0);
			for(int i = 1; i<hand.size();i++)
			{
				Card c1 = (Card)hand.elementAt(i);
				if(c1.getValue() < c.getValue() || (c1.getValue()==c.getValue() && c1.getSuit() < c.getSuit()))
				{
					pos = i;
					c = c1;
				}
			}
			hand.removeElementAt(pos);
			newHand.addElement(c);
		}
		hand = newHand;
	}
	
	public String toString()
	{
		String r = "";
		for(int i = 0; i<hand.size(); i++)
		{
			r = r + hand.elementAt(i).toString()+ " | ";
		}
		
		return r;
	}
}
